package com.java.second_goods.controller;

import com.java.second_goods.common.AjaxResult;
import com.java.second_goods.entity.Coupon;
import com.java.second_goods.entity.UserCoupon;
import com.java.second_goods.service.CouponService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 优惠券信息
 */
@RestController
@RequestMapping("/api/coupon")
@Tag(name = "优惠券管理", description = "优惠券相关接口")
public class CouponController extends BaseController {

    @Autowired
    private CouponService couponService;

    /**
     * 获取优惠券列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取优惠券列表", description = "获取优惠券列表")
    public AjaxResult list(Coupon coupon) {
        List<Coupon> list = couponService.selectCouponList(coupon);
        return success(list);
    }

    /**
     * 获取优惠券详细信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取优惠券详细信息", description = "根据优惠券ID获取优惠券详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(couponService.selectCouponById(id));
    }

    /**
     * 新增优惠券
     */
    @PostMapping
    @Operation(summary = "新增优惠券", description = "新增优惠券")
    public AjaxResult add(@RequestBody Coupon coupon) {
        return toAjax(couponService.insertCoupon(coupon));
    }

    /**
     * 修改优惠券
     */
    @PutMapping
    @Operation(summary = "修改优惠券", description = "修改优惠券")
    public AjaxResult edit(@RequestBody Coupon coupon) {
        return toAjax(couponService.updateCoupon(coupon));
    }

    /**
     * 删除优惠券
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除优惠券", description = "删除优惠券")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(couponService.deleteCouponByIds(ids));
    }

    /**
     * 更新优惠券状态
     */
    @PutMapping("/status/{couponId}/{status}")
    @Operation(summary = "更新优惠券状态", description = "更新优惠券状态")
    public AjaxResult updateStatus(@PathVariable("couponId") Integer couponId,
                                   @PathVariable("status") Integer status) {
        return toAjax(couponService.updateCouponStatus(couponId, status));
    }

    /**
     * 获取有效的优惠券列表
     */
    @GetMapping("/valid")
    @Operation(summary = "获取有效的优惠券列表", description = "获取有效的优惠券列表")
    public AjaxResult getValidCoupons() {
        List<Coupon> coupons = couponService.selectValidCoupons();
        return success(coupons);
    }

    /**
     * 获取用户优惠券列表
     */
    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户优惠券列表", description = "获取用户优惠券列表")
    public AjaxResult getUserCoupons(@PathVariable("userId") Integer userId) {
        List<UserCoupon> coupons = couponService.selectUserCoupons(userId);
        return success(coupons);
    }

    /**
     * 获取用户未使用的优惠券列表
     */
    @GetMapping("/user/{userId}/unused")
    @Operation(summary = "获取用户未使用的优惠券列表", description = "获取用户未使用的优惠券列表")
    public AjaxResult getUnusedUserCoupons(@PathVariable("userId") Integer userId) {
        List<UserCoupon> coupons = couponService.selectUnusedUserCoupons(userId);
        return success(coupons);
    }

    /**
     * 获取用户已使用的优惠券列表
     */
    @GetMapping("/user/{userId}/used")
    @Operation(summary = "获取用户已使用的优惠券列表", description = "获取用户已使用的优惠券列表")
    public AjaxResult getUsedUserCoupons(@PathVariable("userId") Integer userId) {
        List<UserCoupon> coupons = couponService.selectUsedUserCoupons(userId);
        return success(coupons);
    }

    /**
     * 获取可用的优惠券列表
     */
    @GetMapping("/available")
    @Operation(summary = "获取可用的优惠券列表", description = "获取可用的优惠券列表")
    public AjaxResult getAvailableCoupons(@RequestParam("userId") Integer userId,
                                          @RequestParam("amount") BigDecimal amount) {
        List<Coupon> coupons = couponService.selectAvailableCoupons(userId, amount);
        return success(coupons);
    }

    /**
     * 分配优惠券给用户
     */
    @PostMapping("/assign")
    @Operation(summary = "分配优惠券给用户", description = "分配优惠券给用户")
    public AjaxResult assignToUser(@RequestBody Map<String, Object> params) {
        Integer couponId = (Integer) params.get("couponId");
        Integer userId = (Integer) params.get("userId");

        if (couponId == null || userId == null) {
            return error("参数错误");
        }

        int rows = couponService.assignCouponToUser(couponId, userId);
        if (rows > 0) {
            return success("分配成功");
        } else {
            return error("分配失败，优惠券可能不可用或用户已拥有该优惠券");
        }
    }

    /**
     * 使用优惠券
     */
    @PutMapping("/use")
    @Operation(summary = "使用优惠券", description = "使用优惠券")
    public AjaxResult useCoupon(@RequestBody Map<String, Object> params) {
        Integer userCouponId = (Integer) params.get("userCouponId");
        String orderId = (String) params.get("orderId");

        if (userCouponId == null || orderId == null) {
            return error("参数错误");
        }

        int rows = couponService.useCoupon(userCouponId, orderId);
        if (rows > 0) {
            return success("使用成功");
        } else {
            return error("使用失败，优惠券可能已使用或不存在");
        }
    }

    /**
     * 获取用户优惠券详情
     */
    @GetMapping("/userCoupon/{userCouponId}")
    @Operation(summary = "获取用户优惠券详情", description = "获取用户优惠券详情")
    public AjaxResult getUserCouponInfo(@PathVariable("userCouponId") Integer userCouponId) {
        return success(couponService.selectUserCouponById(userCouponId));
    }
}